<?php
/**
 * 公共通讯薄
 * Enter description here ...
 * @author cc
 *
 */
namespace Home\Controller;
use Think\Controller;
class AddressBookController extends CommonController{
	public function index(){
		$model = M("address_book");
		
		$count = $model->count();
		$page = new \Think\Page($count,C("PAGE_LIST_ROWS"));
		$show = $page->show();
		
		$data = $model->limit($page->firstRow.','.$page->listRows)->select();
		foreach ($data as $key=>$value){
			$data[$key]['tel'] = str_replace(";", "</br>", $value['tel']);
			$data[$key]['email'] = str_replace(";", "</br>", $value['email']);
		}
		$this->assign('data',$data);
		$this->assign('page', $show);
		$this->assign("empty", emptyhtml("list"));
		$this->display();
	}
	
	public function lists(){
		$model = M("address_book");
		
		$count = $model->count();
		$page = new \Think\Page($count,C("PAGE_LIST_ROWS"));
		$show = $page->show();
		
		$data = $model->order("id asc")->limit($page->firstRow.','.$page->listRows)->select();
		foreach ($data as $key=>$value){
			$data[$key]['tel'] = str_replace(";", "</br>", $value['tel']);
			$data[$key]['email'] = str_replace(";", "</br>", $value['email']);
		}
		$this->assign('data',$data);
		$this->assign('page', $show);
		$this->assign("empty", emptyhtml("list"));
		$this->display();
	}
	
	public function add(){
		$this->display();
	}
	
	public function insert(){
		if(!IS_POST){ $this->error("非法表单");exit; }
		$data = $_POST;
		$model = D("AddressBook");
		if(!$model->create()){
			$this->error($model->getError());
		}else{
			$success = $model->add( $data );
			if($success){
				$this->systemlog("add","公共通讯薄管理", "添加公共通讯薄---".$data['username']);
				$this->success("人员信息添加成功！", "lists");
			}else{
				$this->error("人员信息添加失败！");
			}
		}
	}
	
	public function edit(){
		$id = I('get.id');
		if(!is_id($id)){ $this->error("非法参数！"); }
		$model = M("address_book");
		$data = $model->where('id=%d',$id)->find();
		$this->assign("data", $data);
		$this->assign("id", $id);
		$this->display();
	}
	
	public function save(){
		if(!IS_POST){ $this->error("非法表单"); }
		$id = I('post.id');
		if(!is_id($id)){ $this->error("非法参数！"); }
		$data = $_POST;
		$model = D("AddressBook");
		if(!$model->create()){
			$this->error($model->getError());
		}else{
			$success = $model->where('id=%d',$id)->save( $data );
			if($success){
				$this->systemlog("edit","公共通讯薄管理", "修改公共通讯薄---".$data['username']);
				$this->success("人员信息修改成功！", "lists");
			}else{
				$this->error("人员信息修改失败！");
			}
		}
	}
	
	public function delete(){
		$id = I('get.id');
		if(!is_id($id)){ $this->error("非法参数！"); }
		$model = M("address_book");
		$username = $model->where('id=d%',$id)->getField("username");
		$success = $model->where('id=%d',$id)->delete();
		if($success){
			$this->systemlog("delete","公共通讯薄管理", "删除公共通讯薄---".$username);
		}
	}
    
    public function _empty($name){
    	$this->emptyaction($name);
    }
}
?>